<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\AdminController;
use App\Models\System\Menus;
use App\Models\System\Config;
use Illuminate\Http\Request;

class IndexController extends AdminController
{
    public function systemMenus(Request $request)
    {
        $userAllPermissions = $this->userAllPermissions($request);
        $list = Menus::getAllMenus();
        $arr = [];
        if ($userAllPermissions && $list) {
            foreach ($list as $value) {
                if (isset($value["name"], array_flip($userAllPermissions)[$value["name"]])) {
                    $arr[] = $this->sysPerData($value);
                }
            }
            $arr = $this->libUtils()->listToTree($arr);
        }
        return $this->succeed([
            'menus' => $this->defaultMenus($arr),
            'permissions' => $userAllPermissions,
            'dashboardGrid' => $this->getDashboardGrid()
        ]);
    }

    private function getDashboardGrid(): array
    {
        return [
            'welcome', 'ver', 'time', 'progress', 'echarts', 'about'
        ];
    }
    private function defaultMenus(array $arr): array
    {
        array_unshift($arr, [
            'name' => "home",
            'path' => "/home",
            'meta' => [
                'title' => "首页",
                'icon' => "el-icon-eleme-filled",
                'type' => "menu",
            ],
            'children' => [
                [
                    'name' => "dashboard",
                    'path' => "/dashboard",
                    'meta' => [
                        'title' => "控制台",
                        'icon' => "el-icon-menu",
                        'affix' => true
                    ],
                    'component' => "home"
                ],
                [
                    'name' => "userCenter",
                    'path' => "/userCenter",
                    'meta' => [
                        'title' => "帐号信息",
                        'icon' => "el-icon-user"
                    ],
                    'component' => "userCenter"
                ],
                [
                    'name' => "about",
                    'path' => "/other/about",
                    'meta' => [
                        'title' => "关于",
                        'icon' => "el-icon-info-filled",
                    ],
                    'component' => "other/about"
                ]
            ]
        ]);
        return $arr;
    }

    public function webSite()
    {
        $config = Config::getPluginSet('website');
        $config['upload'] = $this->getUploadConfig();
        return $this->succeed($config);
    }

}
